home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / ax25dump.c < prev    next >
C/C++ Source or Header  |  1991-02-25  |  4KB  |  192 lines

  1. /* @(#) $Header: ax25dump.c,v 1.5 91/02/24 20:16:33 deyke Exp $ */
  2.  
  3. /* AX25 header tracing
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "ax25.h"
  10. #include "lapb.h"
  11. #include "trace.h"
  12. #include "socket.h"
  13.  
  14. static char *decode_type __ARGS((int type));
  15.  
  16. /* Dump an AX.25 packet header */
  17. void
  18. ax25_dump(fp,bpp,check)
  19. FILE *fp;
  20. struct mbuf **bpp;
  21. int check;      /* Not used */
  22. {
  23.     char tmp[AXBUF];
  24.     char frmr[3];
  25.     int control,pid,seg;
  26.     int16 type;
  27.     int unsegmented;
  28.     struct ax25 hdr;
  29.     char *hp;
  30.  
  31.     fprintf(fp,"AX25: ");
  32.     /* Extract the address header */
  33.     if(ntohax25(&hdr,bpp) < 0){
  34.         /* Something wrong with the header */
  35.         fprintf(fp," bad header!\n");
  36.         return;
  37.     }
  38.     fprintf(fp,"%s",pax25(tmp,hdr.source));
  39.     fprintf(fp,"->%s",pax25(tmp,hdr.dest));
  40.     if(hdr.ndigis > 0){
  41.         fprintf(fp," v");
  42.         for(hp = hdr.digis[0]; hp < &hdr.digis[hdr.ndigis][0];
  43.          hp += AXALEN){
  44.             /* Print digi string */
  45.             fprintf(fp," %s%s",pax25(tmp,hp),
  46.              (hp[ALEN] & REPEATED) ? "*":"");
  47.         }
  48.     }
  49.     if((control = PULLCHAR(bpp)) == -1)
  50.         return;
  51.  
  52.     putc(' ',fp);
  53.     type = ftype(control);
  54.     fprintf(fp,"%s",decode_type(type));
  55.     /* Dump poll/final bit */
  56.     if(control & PF){
  57.         switch(hdr.cmdrsp){
  58.         case LAPB_COMMAND:
  59.             fprintf(fp,"(P)");
  60.             break;
  61.         case LAPB_RESPONSE:
  62.             fprintf(fp,"(F)");
  63.             break;
  64.         default:
  65.             fprintf(fp,"(P/F)");
  66.             break;
  67.         }
  68.     }
  69.     /* Dump sequence numbers */
  70.     if((type & 0x3) != U)   /* I or S frame? */
  71.         fprintf(fp," NR=%d",(control>>5)&7);
  72.     if(type == I || type == UI){
  73.         if(type == I)
  74.             fprintf(fp," NS=%d",(control>>1)&7);
  75.         /* Decode I field */
  76.         if((pid = PULLCHAR(bpp)) != -1){        /* Get pid */
  77.             if(pid == PID_SEGMENT){
  78.                 unsegmented = 0;
  79.                 seg = PULLCHAR(bpp);
  80.                 fprintf(fp,"%s remain %u",seg & SEG_FIRST ?
  81.                  " First seg;" : "",seg & SEG_REM);
  82.                 if(seg & SEG_FIRST)
  83.                     pid = PULLCHAR(bpp);
  84.             } else
  85.                 unsegmented = 1;
  86.  
  87.             switch(pid){
  88.             case PID_SEGMENT:
  89.                 putc('\n',fp);
  90.                 break;  /* Already displayed */
  91.             case PID_ARP:
  92.                 fprintf(fp," pid=ARP\n");
  93.                 arp_dump(fp,bpp);
  94.                 break;
  95.             case PID_NETROM:
  96.                 fprintf(fp," pid=NET/ROM\n");
  97.                 /* Don't verify checksums unless unsegmented */
  98.                 netrom_dump(fp,bpp,unsegmented);
  99.                 break;
  100.             case PID_IP:
  101.                 fprintf(fp," pid=IP\n");
  102.                 /* Don't verify checksums unless unsegmented */
  103.                 ip_dump(fp,bpp,unsegmented);
  104.                 break;
  105.             case PID_X25:
  106.                 fprintf(fp," pid=X.25\n");
  107.                 break;
  108.             case PID_TEXNET:
  109.                 fprintf(fp," pid=TEXNET\n");
  110.                 break;
  111.             case PID_FLEXNET:
  112.                 fprintf(fp," pid=FLEXNET\n");
  113.                 break;
  114.             case PID_NO_L3:
  115.                 fprintf(fp," pid=Text\n");
  116.                 break;
  117.             default:
  118.                 fprintf(fp," pid=0x%x\n",pid);
  119.             }
  120.         }
  121.     } else if(type == FRMR && pullup(bpp,frmr,3) == 3){
  122.         fprintf(fp,": %s",decode_type(ftype(frmr[0])));
  123.         fprintf(fp," Vr = %d Vs = %d",(frmr[1] >> 5) & MMASK,
  124.             (frmr[1] >> 1) & MMASK);
  125.         if(frmr[2] & W)
  126.             fprintf(fp," Invalid control field");
  127.         if(frmr[2] & X)
  128.             fprintf(fp," Illegal I-field");
  129.         if(frmr[2] & Y)
  130.             fprintf(fp," Too-long I-field");
  131.         if(frmr[2] & Z)
  132.             fprintf(fp," Invalid seq number");
  133.         putc('\n',fp);
  134.     } else
  135.         putc('\n',fp);
  136.  
  137. }
  138. static char *
  139. decode_type(type)
  140. int16 type;
  141. {
  142.     switch(type){
  143.     case I:
  144.         return "I";
  145.     case SABM:
  146.         return "SABM";
  147.     case DISC:
  148.         return "DISC";
  149.     case DM:
  150.         return "DM";
  151.     case UA:
  152.         return "UA";
  153.     case RR:
  154.         return "RR";
  155.     case RNR:
  156.         return "RNR";
  157.     case REJ:
  158.         return "REJ";
  159.     case FRMR:
  160.         return "FRMR";
  161.     case UI:
  162.         return "UI";
  163.     default:
  164.         return "[invalid]";
  165.     }
  166. }
  167.  
  168. /* Return 1 if this packet is directed to us, 0 otherwise. Note that
  169.  * this checks only the ultimate destination, not the digipeater field
  170.  */
  171. int
  172. ax_forus(iface,bp)
  173. struct iface *iface;
  174. struct mbuf *bp;
  175. {
  176.     struct mbuf *bpp;
  177.     char dest[AXALEN];
  178.  
  179.     /* Duplicate the destination address */
  180.     if(dup_p(&bpp,bp,0,AXALEN) != AXALEN){
  181.         free_p(bpp);
  182.         return 0;
  183.     }
  184.     if(pullup(&bpp,dest,AXALEN) < AXALEN)
  185.         return 0;
  186.     if(addreq(dest,iface->hwaddr))
  187.         return 1;
  188.     else
  189.         return 0;
  190. }
  191.  
  192.